public class LeetCode643 {
    public static void main(String[] args) {
        LeetCode643 leetCode643 = new LeetCode643();
        int[] nums = {0,4,0,3,2};
        System.out.println(leetCode643.findMaxAverage(nums, 1));
    }

    public double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        double avg = 0;
        // int endIndex = k - 1;
        for(int subIndex = 0; subIndex < k; subIndex++){
            sum += nums[subIndex];
        }

        int maxSum = sum;
        for (int i = k; i < nums.length; i++){

            sum = sum + nums[i] - nums[i - k];
            if(maxSum < sum){
                maxSum = sum;
            }

        }
        avg = (double)maxSum/k;
        return avg;
    }
}
